const fs = require("fs");
const path = require("path");
//创建一个输出目录
const dist = path.join(__dirname, "dist");
function minifyCss(data) {
  //首先判断是否有目标输出文件夹
  if (!fs.existsSync(dist)) {
    //不存在，创建
    fs.mkdirSync(dist);
  }
  //匹配空格和注释清空
  let tempData = data.replace(/\s+/g, "").replace(/\/\*{1,2}.*\*\//, "");
  //写入压缩处理的内容
  fs.writeFile(dist + "/test.css", tempData, (err) => {
    if (err) throw err;
    console.log("文件写入成功");
    fs.rename(dist + "/test.css", dist + "/test.min.css", (err) => {
      if (err) throw err;
      console.log("重命名成功");
    });
  });
}
fs.readFile("dist/test.css", (err, data) => {
  if (err) {
    throw err;
  } else {
    minifyCss(data.toString());
  }
});
